package weekimwee.cn.circleviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**
 * Created by Wee Kim Wee on 2017/10/23.
 */

public class CircleView extends View {
    private int firstColor;
    private int secondColor;
    private int circleWidth;
    private int speed;
    private int textSize;
    private float progress;
    private boolean isNext;
    private String bigCircleText;
    private int bigCircleTextColor;
    private int time;
    private DrawThread drawThread;
    private SimpleDateFormat formatter = new SimpleDateFormat("mm分ss秒");
    private final String LOADING ="已等待";
    private Paint paint;
    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.circle, defStyleAttr, 0);
        int n = a.getIndexCount();
        for(int i = 0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.circle_firstColor:
                    firstColor = a.getColor(attr, Color.GRAY);
                    break;
                case R.styleable.circle_secondColor:
                    secondColor = a.getColor(attr,Color.GRAY);
                    break;
                case R.styleable.circle_circleWidth:
                    circleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.circle_speed:
                    speed=a.getInt(attr, 20);
                    break;
                case R.styleable.circle_textSize:
                    textSize =a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.circle_textColorFirst:
                    bigCircleTextColor = a.getColor(attr,Color.BLACK);
                    break;
            }
        }
        a.recycle();
        bigCircleText = "00分00秒";

        paint = new Paint();
        drawThread = new DrawThread();
        drawThread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //大圆
        int centre = getWidth() / 2; // 获取圆心的x坐标
        int radius = (centre - circleWidth / 2)-25;// 半径
        paint.setStrokeWidth(circleWidth); // 设置圆环的宽度
        paint.setAntiAlias(true); // 消除锯齿
        paint.setStyle(Paint.Style.STROKE); // 设置空心
        RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限

        //跟着跑的小圆
        float h = (float) (2*( Math.PI / 360) * progress);
        float x = (float) ( radius+25+Math.sin(h) * radius);
        float y = (float) (radius+25-Math.cos(h) * radius);
        //在180度位置上的圆
        //float hc = (float) (2*( Math.PI / 360) * 180);
        //float xc = (float) ( radius+25+Math.sin(hc) * radius);
        //float yc = (float) (radius+25-Math.cos(hc) * radius);

        //大圆边框绘制颜色
        if (!isNext) {// 第一颜色的圈完整，第二颜色跑
            paint.setColor(firstColor); // 设置圆环的颜色
            canvas.drawCircle(centre, centre, radius, paint); // 画出圆环
            paint.setColor(secondColor); // 设置圆环的颜色
            canvas.drawArc(oval, -90, progress, false, paint); // 根据进度画圆弧

            paint.setColor(secondColor);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(x,y,10,paint);
            //paint.setColor(firstColor);
            //paint.setStyle(Paint.Style.FILL);
            //canvas.drawCircle(xc,yc,10,paint);
        } else {
            paint.setColor(secondColor); // 设置圆环的颜色
            canvas.drawCircle(centre, centre, radius, paint); // 画出圆环
            paint.setColor(firstColor); // 设置圆环的颜色
            canvas.drawArc(oval, -90, progress, false, paint); // 根据进度画圆弧

            paint.setColor(firstColor);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(x,y,8,paint);
            //paint.setColor(secondColor);
            //paint.setStyle(Paint.Style.FILL);
            //canvas.drawCircle(xc,yc,10,paint);
        }


        paint.setTextSize(textSize);
        //已等待
        float loadingTextWidth = paint.measureText(LOADING);
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(1);
        canvas.drawText(LOADING,centre-loadingTextWidth/2,centre-textSize,paint);
        //几分几秒
        float bigCircleTextWidth = paint.measureText(bigCircleText);
        paint.setColor(bigCircleTextColor);
        canvas.drawText(bigCircleText, centre - bigCircleTextWidth / 2, centre + textSize , paint);
    }

    public void stop(){
        drawThread.close();
    }

    class DrawThread extends Thread {

        private boolean isRun = true;

        @Override
        public void run() {
            while (isRun) {
                BigDecimal b1 = new BigDecimal(Float.toString(progress));
                BigDecimal b2 = new BigDecimal(Float.toString(0.3f));
                progress = b1.add(b2).floatValue();
                if (progress == 360) {
                    progress = 0;
                    if (!isNext)
                        isNext = true;
                    else
                        isNext = false;
                }
                time = time+speed;
                postInvalidate();
                bigCircleText = formatter.format(time);
                try {
                    Thread.sleep(speed);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void close() {
            isRun = false;
        }
    }
}
